﻿using NLog;
using System;
using System.IO;
using System.Windows.Forms;

namespace HyperplaneTech.CdrPlugin {
    public partial class FrmMain : Form {

        /// <summary>
        /// 日志记录器
        /// </summary>
        private static readonly Logger log = NLog.LogManager.GetCurrentClassLogger();

        public FrmMain() {
            InitializeComponent();
        }

        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectSourceFolder_Click(object sender, EventArgs e) {
            try {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件夹";
                DialogResult result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.Cancel) {
                    return;
                }
                string folderPath = dialog.SelectedPath.Trim();
                DirectoryInfo theFolder = new DirectoryInfo(folderPath);
                if (theFolder.Exists) {
                    txtSourcePath.Text = folderPath;
                    log.Info("已选择了监听文件夹：{}", folderPath);
                }
            } catch (Exception ex) {
                log.Error("选择文件夹异常：{}", ex.Message);
            }
        }

        /// <summary>
        /// 窗体加载完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e) {
            try {
                // 设置调试文件夹路径
                string debugPath = "F:\\zebe\\客户需求\\SKY\\其他平台发深圳李灿订单（杨2023-10-10）";
                if (Directory.Exists(debugPath)) {
                    txtSourcePath.Text = debugPath;
                }
                // 读取设置文件
                string settingFile = AppUtil.GetAppSettingFilePath();
                if (File.Exists(settingFile)) {
                    string path = File.ReadAllText(settingFile);
                    txtSourcePath.Text = path;
                    log.Info("加载监听文件夹成功：{}", path);
                }
            } catch (Exception ex) {
                log.Error("窗体加载异常：{}", ex.Message);
            }
        }

        /// <summary>
        /// 单击保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e) {
            try {
                string settingFile = AppUtil.GetAppSettingFilePath();
                string path = txtSourcePath.Text.Trim();
                if (!Directory.Exists(path)) {
                    MessageBox.Show("文件夹不存在，请检查：" + path);
                    return;
                }
                File.WriteAllText(settingFile, path);
                // 自动销毁当前监听器
                if (AppManager.fileSystemWatcher != null) {
                    AppManager.fileSystemWatcher.Dispose();
                }
                // 启动开始监听
                AppManager.StartFileWatch(path);
                MessageBox.Show("保存成功！已重新启动文件夹监听");
                // 关闭窗体
                this.Close();
            } catch (Exception ex) {
                MessageBox.Show("保存发生异常：" + ex.Message);
                log.Error("保存发生异常：{}", ex.Message);
            }
        }

    }
}
